clear
capture clear matrix
set mem 2g		
set maxvar 10000
set matsize 5000
version 13		
set more off		
set scrollbufsize 50000

*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
* SET GLOBAL $PATHS
*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
global root = "INSERT CUSTOMIZED PATH"
global data= "$folder/data"
*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

use "$data/raw/PSID/pequiv_long.dta", clear

mvdecode _all, mv (-8=.)
mvdecode _all, mv (9999=.)


***************
****pequiv****
***************

des d11102LL d11101  d11112LL d11112LL m11102 d11109 i11110 i11102  d11106 d11107
sum d11102LL d11101  d11112LL d11112LL m11102 d11109 i11110 i11102  d11106 d11107

****here we are trying to create the same variables as for the SOEP sample***

tab d11102LL, miss
drop if d11102LL==.m

gen female=0
replace female=1 if d11102LL==2
tab female, miss


*age

tab d11101, miss
drop if d11101==.s
drop if d11101==.m

gen age=d11101

*occupational status

tab e11103, miss
drop if e11103 ==.s

tab e11103
gen fulltimeemployed=0
replace fulltimeemployed=1 if e11103==1

gen parttimeemployed=0
replace parttimeemployed=1 if e11103==2


gen nonemployed=0
replace nonemployed=1 if e11103==3


*Race*
*~~~~*

tab d11112LL , miss nol

gen white=0
replace white=1 if d11112LL==1

gen black=0
replace black=1 if d11112LL==2

gen native=0
replace native=1 if d11112LL==3

gen asian=0
replace asian=1 if d11112LL==4

gen hispanic=0
replace hispanic=1 if d11112LL==5


*Years of education
*~~~~~~~~~~~~~~~~~~~

tab d11109 , miss
*drop if d11109==.m

*33% have missings on this variable but we have to drop them because we slice income dynamics by education later
*best take this variable

tab d11108, miss
drop if d11108==.m

*  Education With |
* Respect to High |
*          School |      Freq.     Percent        Cum.
*-----------------+-----------------------------------
*Less than H S  1 |    142,171       19.26       19.26
*High School    2 |    180,803       24.49       43.75
*More than H S  3 |    165,061       22.36       66.11
*              .m |    250,129       33.89      100.00
*-----------------+-----------------------------------
*           Total |    738,164      100.00


gen dropout=0
replace dropout=1 if d11108==1

gen highschool=0
replace highschool=1 if d11108==2

gen college=0
replace college=1 if d11108==3

sort year
by year: sum dropout
by year: sum highschool
by year: sum college



*Utilization and Insurance~*
*~~~~~~~~~~~~~~~~~~~~~~~~~~*

*Utilization

tab m11101, miss
gen hospitalstay=0
replace hospitalstay=1 if m11101==1

tab m11102, miss
gen hospitalnights = m11102


*******************
****income data****
*******************


*Combined old-age, disability, etc. pensions IOLDY$$ 99
*Combined widows and orphans pension IWIDY$$ 100
*Combined company pension ICOMP$$ 101
*Combined private pension IPRVP$$ 102

/*&for var ijob1-igrv2: replace X=0 if X==-2 
gen o_indtotal=0
for X in var i11110 ioldy  iwidy  iunby  iunay  isuby imaty  istuy  iprvp   ialim  ielse icomp:  replace o_indtotal=o_indtotal+X if X>0 
sum o_indtotal
sort syear
by syear: sum o_indtotal

*Einkommen (annual pre- bzw. post-govt-income jeweils PLUS IR) *
*OECD EQUIVALENCE SCALE 

gen oecd    =1+((d11106-1-h11101)*0.5)+(h11101*0.3) 
tab2 oecd d11106 
gen o_eqpost=(i11102+i11105)/oecd 
sum o_eqpost
by syear: sum o_eqpost*/



*

gen labgro= i11110/12 
sort year
by year: sum labgro

*
*i11102          double  %9.0g                 HH Post-Government Income
*d11106          float   %9.0g                 Number of Persons in HH
*d11107          float   %9.0g                 Number of Children in HH

gen oecd   =1+((d11106-1-d11107)*0.5)+(d11107*0.3) 
tab2 oecd d11106 
replace oecd=1 if oecd<1

gen o_eqpost=(i11113)/oecd 
replace o_eqpost=0 if o_eqpost <0
sum o_eqpost
count if o_eqpost==.


*This is from the OECD databank, CPI deflator for the US.

gen deflator=1
replace deflator=	0.1618	 if year ==	1970
replace deflator=	0.1687	 if year ==	1971
replace deflator=	0.1742	 if year ==	1972
replace deflator=	0.1850	 if year ==	1973
replace deflator=	0.2054	 if year ==	1974
replace deflator=	0.2242	 if year ==	1975
replace deflator=	0.2371	 if year ==	1976
replace deflator=	0.2525	 if year ==	1977
replace deflator=	0.2718	 if year ==	1978
replace deflator=	0.3024	 if year ==	1979
replace deflator=	0.3434	 if year ==	1980
replace deflator=	0.3789	 if year ==	1981
replace deflator=	0.4021	 if year ==	1982
replace deflator=	0.4150	 if year ==	1983
replace deflator=	0.4328	 if year ==	1984
replace deflator=	0.4482	 if year ==	1985
replace deflator=	0.4567	 if year ==	1986
replace deflator=	0.4734	 if year ==	1987
replace deflator=	0.4927	 if year ==	1988
replace deflator=	0.5165	 if year ==	1989
replace deflator=	0.5444	 if year ==	1990
replace deflator=	0.5675	 if year ==	1991
replace deflator=	0.5846	 if year ==	1992
replace deflator=	0.6019	 if year ==	1993
replace deflator=	0.6176	 if year ==	1994
replace deflator=	0.6349	 if year ==	1995
replace deflator=	0.6535	 if year ==	1996
replace deflator=	0.6688	 if year ==	1997
replace deflator=	0.6792	 if year ==	1998
replace deflator=	0.6941	 if year ==	1999
replace deflator=	0.7175	 if year ==	2000
replace deflator=	0.7378	 if year ==	2001
replace deflator=	0.7495	 if year ==	2002
replace deflator=	0.7665	 if year ==	2003
replace deflator=	0.7870	 if year ==	2004
replace deflator=	0.8137	 if year ==	2005
replace deflator=	0.8400	 if year ==	2006
replace deflator=	0.8639	 if year ==	2007
replace deflator=	0.8971	 if year ==	2008
replace deflator=	0.8939	 if year ==	2009
replace deflator=	0.9086	 if year ==	2010
replace deflator=	0.9372	 if year ==	2011
replace deflator=	0.9566	 if year ==	2012
replace deflator=	0.9706	 if year ==	2013
replace deflator=	0.9864	 if year ==	2014
replace deflator=	0.9876	 if year ==	2015
replace deflator=	1.0000	 if year ==	2016

tab deflator, miss


***********************************************************
*****NOW INFLATE INCOME VARIABLES TO 2016 Dollars**********
***********************************************************

gen labgroII=(labgro/deflator)
gen labnetII=(labnet/deflator)
gen o_indtotalII=(o_indtotal/deflator)
gen o_eqpostII=(o_eqpost/deflator)


*save "$DATADIR\PSID1984_2015.dta", replace




label variable x11101LL  "Enrollee ID"
label variable year "Year"
label variable female "Female"
label variable white "Self-identified white"
label variable black  "Self-identified black"
label variable native  "Self-identified native"
label variable asian  "Self-identified asian"
label variable hispanic  "Self-identified hispanic"

label variable labgro "Self-reported individual gross wages in dollars of given year, PSID"
label variable o_eqpost "Equivalized post-tax post-transfer income in dollars of given year, PSID"
label variable oecd "OECD equivalent score (considering household members)"
label variable deflator "Deflator to inflate moneary euro values to 2016 dollars"
label variable labgroII "Labgro but inflated to 2016 dollars"
label variable o_eqpostII "o_eqpostII but inflated to 2016 dollars"

label variable dropout "Less than high school"
label variable highschool "High school degree"
label variable college "More than highschool in original data"

label variable age "Age of respondent"

label variable hospitalstay "Binary, 1 if hospital stay in past calendar year"
label variable hospitalnights "Number of hospital nights in past calendar year"

label variable fulltimeemployed "Binary, 1 if currently full-time employed "
label variable parttimeemployed  "Binary, 1 if currently part-time employed"
label variable nonemployed  "Binary, 1 if currently non employed"

************************
***SUMMARY STATISTIC***
***(Not in paper)******
***********************

global xvar "female age fulltimeemployed parttimeemployed nonemployed white black dropout highschool college labgro labgroII oecd o_eqpost o_eqpostII" 

sum $xvar
*sutex $xvar, nobs labels minmax digits(4) title(Descriptive Statistics) key(DesStat) longtable file("DesStat.tex") replace


***Sample Selection***

drop if age<26
drop if year<1984

keep year x11101LL l11101 female age fulltimeemployed parttimeemployed nonemployed white black asian hispanic dropout highschool college hospitalstay hospitalnights labgro oecd o_eqpost labgroII o_eqpostII
tabulate year, summarize(o_eqpostII) means

compress
save "$data/temp/PSID1984_2015_short.dta", replace

clear
exit


